package demo6;

class Animals {
    public String name;
    public int age;
    public Animals(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public void eat(String food) {
        System.out.println("我是一只小动物");
        System.out.println(this.name + "正在吃" + food);
    }
}

class Bird extends Animals {
    public Bird(String name,int age) {
        super(name,age);
    }
    public void eat(String food) {
        System.out.println("我是一只小鸟");
        System.out.println(this.name + "正在吃" + food);
    }
    public void fly() {
        System.out.println("我是一只小鸟");
        System.out.println(this.name + "正在飞行！！！");
    }
}
class Dog extends Animals {
    public Dog(String name,int age) {
        super(name,age);
    }
    public void eat(String food) {
        System.out.println("我是一只小狗");
        System.out.println(this.name + "正在吃" + food);
    }
    public void run() {
        System.out.println("我是一只小狗");
        System.out.println(this.name + "正在奔跑！！！");
    }
}


public class Text {
    public static void main(String[] args) {
//        Animals animals = new Dog("小黄",8);
//        animals.eat("狗粮");
//        Dog dog = (Dog) animals;
//        dog.run();
        Animals animals = new Dog("小黄",8);
        animals.eat("狗粮");
        if(animals instanceof Dog) {
            Dog dog = (Dog) animals;//因为向下转型涉及到强制类型转换，所以
                                    //使用 instanceof 来判定 animals 是不是 Dog类型
            dog.run();
        }else{
            Bird bird = (Bird) animals;
            bird.fly();
        }
    }
}
